package com.ht.sps.nh.module;

import com.ht.sps.bui.model.SimpleTreeNode;
import com.ht.sps.nh.entity.TreeMenu;
import com.ht.sps.nh.service.TreeMenuService;
import com.ht.sps.util.ValidationUtil;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.View;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 硬件设置模块
 * @author: 薄成文 13-10-24 12:57
 * To change this template use File | Settings | File Templates.
 */
@IocBean
@At("/nh/system/treeMenuSetting")
public class TreeMenuSettingModule {
    private static final Logger log = LoggerFactory.getLogger(TreeMenuSettingModule.class);

    //树菜单
    @Inject
    private TreeMenuService treeMenuService;


    /**
     * 硬件设置页面入口
     * @param request
     */
    @At("/")
    @Ok("th:nh/system/treeMenuSetting")
    public void index(HttpServletRequest request) {

        //设置树初始化数据 （第一层及区域控制器）
        List<SimpleTreeNode> trees = getAllTreeMenu();
        List<TreeMenu> tmList = getParentTreeMenuList();
        request.setAttribute("tmTree", trees);
        request.setAttribute("tmList", tmList);
    }


    /**
     * 树数据加载服务接口
     *
     * @param id
     *          参数结构： 区域控制器ID[-门控器地址]
     * @return
     *         返回BUI树结构数据 ID数据结构：区域控制器ID[-门控器地址][-类型（门:1;输入:2；继电器:3；读卡器:4）]]
     */
    @At
    @Ok("fastjson")
    public  TreeMenu getTreeMenu(@Param("id")Long id){
        TreeMenu tm = treeMenuService.fetch(id);
        return tm;
    }

    /**
     * 获得所有菜单
     * @return
     *          返回所有菜单BUI树结构数据
     */
    @At
    @Ok("fastjson")
    public  List<SimpleTreeNode> getAllTreeMenu(){
        List<SimpleTreeNode> treeList = new ArrayList();
        //获得所有区域控制器数据
        List<TreeMenu> tmList = treeMenuService.findAll();
        //格式化树控件数据
        if(tmList != null && tmList.size() > 0){
            for(TreeMenu tm : tmList){
                SimpleTreeNode node = new SimpleTreeNode(String.valueOf(tm.getParentId()),String.valueOf(tm.getId()),tm.getMenuName(),treeMenuService.isLeaf(tm.getId()));
                treeList.add(node);
            }
        }
        return treeList;
    }

    @At
    @Ok("fastjson")
    public List<SimpleTreeNode> insertOrUpdate(HttpServletRequest request,@Param("..")TreeMenu treeMenu){
        if (treeMenu.getId()!=null){
            TreeMenu tm1 = treeMenuService.fetch(treeMenu.getId());
            if(treeMenu.getParentId()==0){
                tm1.setMenuLevel(1);
            }else{
                TreeMenu tmParent = treeMenuService.fetch(treeMenu.getParentId());
                tm1.setMenuLevel(tmParent.getMenuLevel()+1);
            }
            tm1.setMenuName(treeMenu.getMenuName());
            tm1.setModelCount(treeMenu.getModelCount());
            tm1.setParentId(treeMenu.getParentId());
            treeMenuService.update(tm1);
        } else {
            if(treeMenu.getParentId()==0){
                treeMenu.setMenuLevel(1);
            }else{
                TreeMenu tmParent = treeMenuService.fetch(treeMenu.getParentId());
                treeMenu.setMenuLevel(tmParent.getMenuLevel()+1);
            }
            treeMenuService.insert(treeMenu);
        }
        List<SimpleTreeNode> list = getAllTreeMenu();
        return list;

    }

    @At
    @Ok("fastjson")
    public List<SimpleTreeNode> deleteTreeMenu(HttpServletRequest request,@Param("id")long id){
        treeMenuService.delTreeMenu(id);
        List<SimpleTreeNode> list = getAllTreeMenu();
        return list;
    }

    @At
    @Ok("fastjson")
    public List<TreeMenu> getParentTreeMenuList(){
        return treeMenuService.getParentTreeMenuList();
    }

}
